package com.wy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wy.dto.GoodsInfoDto;
import com.wy.entity.GoodsImage;
import com.wy.entity.GoodsInfo;
import com.wy.entity.GoodsOrder;
import com.wy.mapper.GoodsAuctionMapper;
import com.wy.mapper.GoodsImageMapper;
import com.wy.mapper.GoodsInfoMapper;
import com.wy.service.GoodsInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

import static org.springframework.http.RequestEntity.put;

/**
 * @Author:王渝
 * @Date: 2022/11/30  11:17
 * @Version 1.0
 */
@Slf4j
@Service
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements GoodsInfoService {



    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Autowired
    private GoodsImageMapper goodsImageMapper;

    @Autowired
    private GoodsAuctionMapper goodsAuctionMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${file.url}")
    private String url;

    @Override
    public PageInfo<GoodsInfo> queryPage(int currentPage, int pageSize, String status) {
        PageHelper.startPage(currentPage,pageSize);
        List<GoodsInfo> goodsInfos = goodsInfoMapper.queryAllByStatus(status);
        PageInfo<GoodsInfo> pageInfo = new PageInfo<>(goodsInfos);
        return pageInfo;
    }

    @Override
    public PageInfo<GoodsInfo> queryPageByUserId(int currentPage, int pageSize, String status, Long userId) {

        PageHelper.startPage(currentPage,pageSize);
        List<GoodsInfo> goodsInfos = goodsInfoMapper.queryAllByStatusAndUserId(status,userId);
        PageInfo<GoodsInfo> pageInfo = new PageInfo<>(goodsInfos);
        return pageInfo;
    }

    @Override
    public GoodsInfoDto queryDetailById(Long goodsId) {
        GoodsInfoDto goodsInfoDto = goodsInfoMapper.queryDetailById(goodsId);
        LambdaQueryWrapper<GoodsImage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(GoodsImage::getImage);
        queryWrapper.eq(GoodsImage::getGoodsId,goodsId);
        List<GoodsImage> images = goodsImageMapper.selectList(queryWrapper);
        List<String> urls = new ArrayList<>();
        for (GoodsImage image : images) {
            urls.add(url+image.getImage());
        }
        goodsInfoDto.setImages(urls);
        return goodsInfoDto;
    }

    @Override
    public GoodsInfoDto queryDetailById2(Long goodsId) {

        GoodsInfoDto goodsInfoDto = goodsInfoMapper.queryDetailById(goodsId);
        LambdaQueryWrapper<GoodsImage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(GoodsImage::getImage);
        queryWrapper.eq(GoodsImage::getGoodsId,goodsId);
        List<GoodsImage> images = goodsImageMapper.selectList(queryWrapper);
        List<String> urls = new ArrayList<>();
        for (GoodsImage image : images) {
            urls.add(url+image.getImage());
        }
        GoodsInfoDto goodsInfoDto1 = goodsAuctionMapper.queryCountAndMax(goodsId);

        goodsInfoDto.setCount(goodsInfoDto1.getCount());
        if(goodsInfoDto1.getCount() != 0){
            goodsInfoDto.setMaxAmount(goodsInfoDto1.getMaxAmount());
        }else {
            goodsInfoDto.setMaxAmount(goodsInfoDto.getStartingPrice());
        }

        goodsInfoDto.setImages(urls);
        return goodsInfoDto;
    }

    @Override
    public PageInfo<GoodsInfo> queryPageByType(int currentPage, int pageSize, String type) {
        HashOperations hashOperations = redisTemplate.opsForHash();
        PageInfo<GoodsInfo> goodsInfo1 = (PageInfo<GoodsInfo>) hashOperations.get("goodsInfo" , currentPage + ":" + pageSize+ ":" + type );
        if(goodsInfo1 != null){
            return goodsInfo1;
        }
        PageHelper.startPage(currentPage,pageSize);
        List<GoodsInfo> goodsInfo = goodsInfoMapper.queryAllByType(type);
        PageInfo<GoodsInfo> pageInfo = new PageInfo<>(goodsInfo);
        List<GoodsInfo> list = pageInfo.getList();
        //替换图片名称
        for (GoodsInfo info : list) {
            info.setImage(url + info.getImage());
        }
        pageInfo.setList(list);
        hashOperations.put( "goodsInfo" , currentPage + ":" + pageSize+ ":" + type ,pageInfo);
        return pageInfo;
    }




    @Transactional(rollbackFor = {})
    @Override
    public boolean addGoods(GoodsInfo goodsInfo) {
        String[] images = goodsInfo.getImages();
        int insert = goodsInfoMapper.insert(goodsInfo);
        if(insert < 1){
            return false;
        }
        List<String> list = new ArrayList<>(images.length);
        Collections.addAll(list,images);
        int i = goodsImageMapper.insertBatch(list, goodsInfo.getGoodsId());
        if(i < 1 ){
            return false;
        }
        return true;
    }

    @Transactional(rollbackFor = {})
    @Override
    public int updateStatus() {

        ReentrantLock lock = new ReentrantLock();
        lock.lock();
        try{
            int count = goodsInfoMapper.updateStatusSold1();
            if(count > 0){
                redisTemplate.opsForHash().delete("goodsInfo");
            }
        }finally {
            lock.unlock();
        }
        lock.lock();
        try{
            int count2 = goodsInfoMapper.updateStatusSold2();
            if(count2 > 0){
                redisTemplate.delete("goodsInfo");
            }
        }finally {
            lock.unlock();
        }
        return 0;
    }

    @Override
    public String queryEmailByGoodsId(Long goodsId) {
        return goodsInfoMapper.queryUserInfoByGoodsId(goodsId);
    }

    @Override
    public BigDecimal queryMaxPrice(Long goodsId) {
        return goodsInfoMapper.queryMaxPriceByGoodsId(goodsId);
    }

    @Override
    public PageInfo<GoodsInfo> queryPageByUserId(int currentPage, int pageSize, Long userId) {
        PageHelper.startPage(currentPage,pageSize);
        List<GoodsInfo> goodsInfo = goodsInfoMapper.queryAllByUserId(userId);
        PageInfo<GoodsInfo> pageInfo = new PageInfo<>(goodsInfo);
        List<GoodsInfo> list = pageInfo.getList();
        //替换图片名称
        for (GoodsInfo info : list) {
            info.setImage(url + info.getImage());
        }
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<GoodsInfo> queryPageByUserId2(int currentPage, int pageSize, Long userId) {
        PageHelper.startPage(currentPage,pageSize);
        List<GoodsInfo> goodsInfo = goodsInfoMapper.queryAllByUserId2(userId);
        PageInfo<GoodsInfo> pageInfo = new PageInfo<>(goodsInfo);
        List<GoodsInfo> list = pageInfo.getList();
        //替换图片名称
        for (GoodsInfo info : list) {
            info.setImage(url + info.getImage());
        }
        pageInfo.setList(list);
        return pageInfo;
    }
}
